cmake_minimum_required(VERSION 3.0)
project(utf)

# 设定 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 添加覆盖率支持 - 仅在Debug模式下启用
if(CMAKE_BUILD_TYPE MATCHES Debug)
    message(STATUS "Coverage enabled")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
    
    # 查找lcov工具
    find_program(LCOV_PATH lcov)
    find_program(GENHTML_PATH genhtml)
    
    if(LCOV_PATH AND GENHTML_PATH)
        message(STATUS "lcov and genhtml found - coverage targets enabled")
        
        # 添加覆盖率清理目标
        add_custom_target(coverage-clean
            COMMAND ${LCOV_PATH} --directory . --zerocounters
            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
            COMMENT "Resetting code coverage counters"
        )
        
        # 添加覆盖率收集目标
        add_custom_target(coverage
            COMMAND ${LCOV_PATH} --directory . --capture --output-file coverage.info
            COMMAND ${LCOV_PATH} --remove coverage.info '/usr/include/*' '${PROJECT_SOURCE_DIR}/extern/*' 'test/*' --output-file coverage_filtered.info
            COMMAND ${GENHTML_PATH} -o coverage_report coverage_filtered.info
            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
            COMMENT "Generating code coverage report"
            DEPENDS utf8_check_tests
        )
    else()
        message(WARNING "lcov or genhtml not found - coverage targets disabled")
    endif()
endif()

include_directories(./utf/include)

# 引入 FetchContent 拉取 googletest
include(FetchContent)
FetchContent_Declare(
    googletest
    SOURCE_DIR ${PROJECT_SOURCE_DIR}/extern/googletest-1.14.0
)

# 强制 gtest 使用与主项目一致的 CRT（Windows 下避免运行时冲突）
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# 下载、编译 googletest
FetchContent_MakeAvailable(googletest)

# 启用测试功能
enable_testing()

# 添加子目录（test 目录的 CMake 会处理测试目标）
add_subdirectory(test)